//
//  JXResponse.swift
//  Demo
//
//  Created by xiu on 2020/4/20.
//  Copyright © 2020 111. All rights reserved.
//

import UIKit
import Alamofire
import ObjectMapper
import YJEncry

public class JXResponse: NSObject {
    public var request: URLRequest?
    public var response: HTTPURLResponse?
    public var data: Any?
    public var error: Swift.Error?
    public var isSuccess: Bool {
        return data != nil && error == nil
    }
    
    private override init() {
    }
    
    init(response: AFDataResponse<Any>) {
        
        self.request = response.request
        self.response = response.response
        
        switch response.result {
        case .success(let value):
            self.data = value
            break
        case .failure(let error):
            self.error = error
            break
        }
        
    }
    
    init(response: AFDataResponse<Data>) {
        
        self.request = response.request
        self.response = response.response
        
        switch response.result {
        case .success(let value):
            self.data = value
            break
        case .failure(let error):
            self.error = error
            break
        }
        
    }
    
    public static func requestError() -> JXResponse {
        let response = JXResponse()
        return response
    }
    
    private func getDataDic<T>() -> T? {
           guard let dic = self.data as? Dictionary<String, Any> else {
               return nil
           }
           
           if let dataDict = dic["data"] as? [String: Any] {
               if let data = dataDict["data"] as? T {
                   return data
               }
           }
           
           guard let data = dic["data"] as? T else {
               return nil
           }
       
           return data
       }
    
    public func resolveBaseDicValue() -> [String : Any]? {
        guard let dic = self.data as? Dictionary<String, Any> else {
            return nil
        }
        return dic
    }
    
    public func resolveJsonValue() -> String? {
        guard let dic = self.data as? Dictionary<String, Any> else {
            return nil
        }
        guard JSONSerialization.isValidJSONObject(dic), let data = try? JSONSerialization.data(withJSONObject: dic, options: []) else {
            return nil
        }
        return String(data: data, encoding: .utf8)
    }
    
    public func resolveStringValue() -> String? {
        guard let data:String = getDataDic()  else {
            return nil
        }
        return data
    }
    
    public func resolveDicValue() -> [String : Any]? {
        guard let json:[String : Any] = getDataDic()  else {
            return [:]
        }
        return json
    }
    
    public func resolveListValue() -> [[String : Any]]? {
        guard let json:[[String : Any]] = getDataDic()  else {
            return []
        }
        return json
    }
    
    public func resolveAESDicValue() -> [String : Any]? {
        guard let dic = self.data as? Dictionary<String, Any> else {
            return nil
        }
        
        guard let cryptAES:String = dic["Enc"] as? String else {
            return nil
        }
        
        let encryptAES:String = TokenGenerator.neu_decryptAESData(cryptAES)
        
        guard let data = encryptAES.data(using: .utf8), let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) else {
            return nil
        }
        
        guard let json:[String : Any] = jsonObject as? [String : Any]  else {
            return [:]
        }
        
        return json
    }
    
    public func resolvePotatoDicValue() -> [String : Any]? {
        guard let dic = self.resolveAESDicValue() else {
            return nil
        }
        
        guard let datas:[String : Any] = dic["datas"] as? [String : Any] else {
            return nil
        }
        
        return datas
    }
}
